﻿package ascript.components.collision
{
	//#########################
	// Header Includes
	//#########################
	import ascript.Game;
	import ascript.GameComponent;
	import ascript.GameObject;
	import Box2D.Dynamics.b2Fixture;
	
	//#########################
	// GeneralModifiers Class
	//#########################
	public class CollisionBase extends GameComponent
	{
		//=========================
		// Constructors
		//=========================	
		public function CollisionBase():void
		{
		}
		
		//=========================
		// Members
		//=========================
		override public function initComponent( tGame:Game, tObject:GameObject ):void
		{
			// Set Default Attributes
			defaultAttribute( tObject, "CollisionAcceptedID", new Vector.<String> );
			defaultAttribute( tObject, "CollisionIsEnabled", true );
		}
		
		public function handleCollision( tObject:GameObject, tObjectHit:GameObject, tFixture:b2Fixture, tFixtureHit:b2Fixture, tIsTouching:Boolean ):Boolean
		{
			var tempTouched:Boolean = false;
						
			if ( tObject.getAttribute( "CollisionIsEnabled" ) )
			{
				var tempListLength:uint = ( tObject.getAttribute( "CollisionAcceptedID" ) ).length;
				for ( var i:uint = 0; i < tempListLength; i++ )
				{
					if ( tObject.getAttribute( "CollisionAcceptedID" )[i] == tObjectHit.getAttribute( "CollisionID" ) )
					{
						tempTouched = true;
						break;
					}
				}
			}
			
			return tempTouched;
		}
		
		public function solveCollision( tObject:GameObject, tObjectHit:GameObject, tFixture:b2Fixture, tFixtureHit:b2Fixture, tIsPresolve:Boolean ):Boolean
		{
			if ( tObject.getAttribute( "CollisionIsEnabled" ) )
			{
				var tempListLength:uint = ( tObject.getAttribute( "CollisionAcceptedID" ) ).length;
				for ( var i:uint = 0; i < tempListLength; i++ )
				{
					if ( tObject.getAttribute( "CollisionAcceptedID" )[i] == tObjectHit.getAttribute( "CollisionID" ) )
					{
						return true;
					}
				}
			}
			
			return false;
		}
	}
}